package nl.hva.web.mvcwebapp.controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import nl.hva.web.mvcwebapp.models.User;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class LoginController extends HttpServlet {

    private String address = "/login/login.jsp";
    ArrayList paths_jsp = new ArrayList();
    ArrayList paths = new ArrayList();

    /*
     * HTTP GET request
     */
    
    public void addPaths(){
        paths_jsp.add(0, "/login/login.jsp");
        paths.add(0, "/login");
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        addPaths();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        

        for (int i = 0; i < paths.size(); i++) {
            try {
                String substringpath = path.substring(0, paths.get(i).toString().length());
                if (substringpath.equals(paths.get(i).toString())) {
                    //gebruikerslijst.add(new User(0, "administrator", "admin1", "admin123", "admin", "istrator", "admin@admin.admin", "The Netherlands", "http://www.veryicon.com/icon/png/System/General%20Media/Administrator.png"));
                    // gebruikerslijst.add(new User(1, "Student", "thijsl", "thijslll", "Thijs", "Limmen", "Thijslimmen@hotmail.com", "The Netherlands", "http://www.ct4me.net/images/dmbtest.gif"));
                    //gebruikerslijst.add(new User(2, "Teacher", "teacher1", "teacher123", "tea", "cher", "teacher@teacher.teacher", "England", "http://www.optcorp.com/images2/articles/full-einstein.jpg"));

                    // /Users
                    if (i == 0) {
                        List<User> gebruikerslijst = session.createCriteria(User.class).list();
                        tx.commit();
                        request.setAttribute("pageTitle", "Login");
                        request.setAttribute("gebruikersUitSessie", gebruikerslijst);
                        request.setAttribute("aantalGebruikers", gebruikerslijst.size());
                    }

                    address = paths_jsp.get(i).toString();
                }
            } catch (Exception E) {
            }
        }

        request.setAttribute("path", path);
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.sendRedirect("../index");
    }

    public User getUserFromRequest(HttpServletRequest request) {
        User user = new User();
        if (request.getParameter("id") != null && !request.getParameter("id").isEmpty()) {
            user.setUserId(Integer.parseInt(request.getParameter("id")));
        }
        if (request.getParameter("username") != null) {
            user.setUserFirstName(request.getParameter("username"));
        }
        if (request.getParameter("password") != null) {
            user.setUserLastName(request.getParameter("password"));
        }
        if (request.getParameter("soortuser") != null) {
            user.setUserType(Integer.parseInt(request.getParameter("soortuser")));
        }

        return user;
    }
}
